import {
  tagList,
  general,
  tactic,
  stratagem,
  politics,
  land,
  mainControl,
} from "@/utils/param";
import store from "@/store";

//下一个
export function nextPlayer() {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let id = gameContral.playerId + 1
  let num = (id) % gameContral.playerNum;
  gameContral.playerId = num;
  gameContral.turn = gameContral.turn + 1;
  store.commit("SET_gameContral", gameContral);
  turnStart();
}
// 玩家回合开始
export function turnStart() {
  //  检测是否在自己首都
  //  政策结算
  //  政策部署
  //  移动
  //  经过结算
  //  停留结算
  //  插入战斗/购买/支付/收钱
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));

  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));

  let str = "——玩家【" + playerContral[gameContral.playerId].name + "】回合";

  console.log('开始了回合', gameContral, playerContral);

  addLog(str);

  // if (
  //   playerContral[gameContral.playerId].address ==
  //   playerContral[gameContral.playerId].capital
  // ) {
  //   console.log("政策结算");
  // getPolitics().then((res) => {
  //   console.log("政策部署");
  // gameContral.setting = true;
  // gameContral.showpolitics = true;
  // store.commit("SET_gameContral", gameContral);
  // });
  // } else {
  // }
}
// 处理政策收益-加钱
export function getPolitics() {
  return new Promise((resolve, reject) => {
    let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
    let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
    let counsellor = playerContral[gameContral.playerId].counsellor;
    let chief = playerContral[gameContral.playerId].chief;
    let politics = playerContral[gameContral.playerId].politics;
    let all = 0;
    let double = 1;
    //军师收益
    for (var i = 0; i < counsellor.length; i++) {
      all += counsellor[i].int * 1000;
    }
    //政策收益-屯田
    for (var j = 0; j < counsellor.length; j++) {
      if (politics[j].zhengce == 99) {
        all += 1000;
      }
      if (politics[j].zhengce == 100) {
        double++;
      }
    }
    changeMoney(gameContral.playerId, all * double);
    resolve();
  });
}
// 钱 增改
export function changeMoney(playerId, num) {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  playerContral[playerId].money =
    Number(playerContral[playerId].money) + Number(num);

  let turn = gameContral.turn;
  let math = num >= 0 ? "获得" : "失去";
  let str =
    "玩家【" + playerContral[playerId].name + "】" + math + " " + num + " 金钱";
  addLog(str, turn);
  store.commit("SET_playerContral", playerContral);
}
// 城池 购买
export function buyLand(address, playerId) {
  console.log("购买城池", address, playerId);
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  if (!playerId) {
    playerId = gameContral.playerId;
  }
  let lands = JSON.parse(JSON.stringify(store.getters.land));
  console.log("城池", lands);
  // 扣钱
  changeMoney(playerId, 0 - Number(lands[address].price));
  // 加城
  getLand(playerId, address);
  // 日志
  let turn = gameContral.turn;
  let math = "购买了";
  let str =
    "玩家【" +
    playerContral[playerId].name +
    "】" +
    math +
    " " +
    lands[address].name;
  addLog(str, turn);
}
// 城池 增
export function getLand(playerId = 99, address) {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  let lands = JSON.parse(JSON.stringify(store.getters.land));
  if (playerId == 99) {
    playerId = Number(gameContral.playerId);
  } else {
    playerId = Number(playerId);
  }
  // if (playerContral[playerId].land) {
  playerContral[playerId].land.push(address);
  // } else {
  //   playerContral[playerId].land = [];
  //   playerContral[playerId].land.push(address);
  // }
  //清除驻守武将
  lands[address].defend = [];
  //武将状态改变
  lands[address].holder = playerId;

  store.commit("SET_playerContral", playerContral);
  store.commit("SET_land", lands);
}
// 城池 删
export function loseLand(playerId = 99, address) {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  let lands = JSON.parse(JSON.stringify(store.getters.land));
  if (playerId == 99) {
    playerId = gameContral.playerId;
  }
  let filteredArray = playerContral[playerId].land.filter((item) => {
    return item.id != address;
  });
  playerContral[playerId].land = filteredArray;
  //清除驻守武将
  lands.defend = [];
  //武将状态改变
  lands[address].holder = playerId;

  store.commit("SET_playerContral", playerContral);
  store.commit("SET_land", lands);
}
// 升级城池 改
export function updateLand(playerId, address) {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  let lands = JSON.parse(JSON.stringify(store.getters.land));
  if (lands[addrebss].level + 1 == lands[addrebss].upgradation.length) {
    return;
  }
  //扣钱
  changeMoney(playerId, 0 - Number(lands[address].upgradeCost));
  lands[addrebss].level++;
  //日志
  let turn = gameContral.turn;
  let math = "升级了";
  let str =
    "玩家【" +
    playerContral[playerId].name +
    "】" +
    math +
    " " +
    lands[address].name;
  addLog(str, turn);
  store.commit("SET_playerContral", playerContral);
  store.commit("SET_land", lands);
}
/**
 * 获得武将
 * num 数量
 * playerId 玩家id
 * max 最高稀有度
 * min 最低稀有度
 */
export function getGeneralToPlayer(playerId, num = 1, min = 0, max = 4) {
  let allGeneral = JSON.parse(JSON.stringify(store.getters.general));
  let newplayerContral = [];
  if (store.getters.playerContral) {
    newplayerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  } else {
    newplayerContral = [];
  }
  for (var i = 0; i < num; i++) {
    let general = allGeneral.filter((item) => {
      return item.rarity >= min && item.rarity <= max && item.belong == null;
    });
    let num = Math.floor(Math.random() * general.length);

    allGeneral.forEach((ele) => {
      if (ele.id == general[num].id) {
        ele.belong = playerId;
        if (newplayerContral[playerId].general) {
          newplayerContral[playerId].general.unshift(ele.id);
        } else {
          newplayerContral[playerId].general = [];
          newplayerContral[playerId].general.unshift(ele.id);
        }
      }
    });
  }
  console.log('SET_playerContral1', newplayerContral);
  store.commit("SET_general", allGeneral);
  store.commit("SET_playerContral", newplayerContral);
}
//武将驻守城池
export function setDefend(array, address) {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  let allGeneral = JSON.parse(JSON.stringify(store.getters.general));

  allGeneral.forEach((element) => {
    if (array.includes(element.id)) {
      element.defendTo = address;
    }
  });

  store.commit("SET_general", allGeneral);
}
//武将解除驻守城池
export function outDefend(array, address) {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  let lands = JSON.parse(JSON.stringify(store.getters.land));
  if (lands[addrebss].level + 1 == lands[addrebss].upgradation.length) {
    return;
  }
  //扣钱
  changeMoney(playerId, 0 - Number(lands[address].upgradeCost));
  lands[addrebss].level++;
  //日志
  let turn = gameContral.turn;
  let math = "升级了";
  let str =
    "玩家【" +
    playerContral[playerId].name +
    "】" +
    math +
    " " +
    lands[address].name;
  addLog(str, turn);
  store.commit("SET_playerContral", playerContral);
  store.commit("SET_land", lands);
}
//钱够不够？
export function checkMoney(num, playerId = 99) {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  if (playerId == 99) {
    playerId = gameContral.playerId;
  }
  return playerContral[playerId].money >= num;
}
//是不是自家领地呀？
export function ifTerritory(address, playerId = 99) {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  let playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  if (playerId == 99) {
    playerId = gameContral.playerId;
  }
  return (
    playerContral[playerId].land.includes(address + "") ||
    playerContral[playerId].land.includes(Number(address))
  );
}
//日志
export function addLog(str, turn) {
  let historyInfo = JSON.parse(JSON.stringify(store.getters.historyInfo)) || [];
  console.log(str, turn);
  historyInfo.push({
    str: str,
    turn,
  });
  store.commit("SET_historyInfo", historyInfo);
}
//关闭窗口
export function closePop(key) {
  let gameContral = JSON.parse(JSON.stringify(store.getters.gameContral));
  gameContral.setting = false;
  gameContral[key] = false;
  store.commit("SET_gameContral", gameContral);
}
// 查询
/**当前玩家数据
 * id 玩家id
 * key 关键词
 * 不传返回全部
 * */
export function getCurrentPlayerInfo(id, key) {
  let playerContral = [];
  if (store.getters.playerContral) {
    playerContral = JSON.parse(JSON.stringify(store.getters.playerContral));
  } else {
    playerContral = [];
  }
  if (key && playerContral && playerContral[id]) {
    return playerContral[id][key];
  } else if (playerContral) {
    return playerContral;
  }
}
/**
 * 根据id数组获得数据
 * key 查询字段
 * command 指挥
 * general 武将
 * land 城池
 * player 玩家
 * politics 政策
 * stratagem 计谋
 * tactic 战法
 */
export function getLocalInfoById(array = [], key) {
  console.log("根据id数组获得数据-array:", array, "key", key);
  let allGeneral = [];
  let res = [];
  // allGeneral = JSON.parse(localStorage.getItem("fileDataList"))[key];
  allGeneral = store.getters[key];

  res = allGeneral.filter((item) => {
    console.log(array, "item.id:", item.id);
    return array.includes(Number(item.id)) || array.includes(item.id + "");
  });
  console.log("resresresres", res);
  return res;
}
